package com.gitee.aurora.comm;

import com.gitee.aurora.utils.MapToObj;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class AuroraJdbcTemplate extends JdbcTemplate {

    public AuroraJdbcTemplate(DataSource dataSource) {
        super(dataSource);
    }

    public <T> List<T> queryForMapToList(String sql, Class<T> elementType) throws Exception {
        List<T> arrayList = new ArrayList<>();
        List<Map<String, Object>> maps = super.queryForList(sql);
        for (Map<String, Object> m : maps) {
            T t = MapToObj.mapToObj(m, elementType);
            arrayList.add(t);
        }
        return arrayList;
    }

    public <T> List<T> queryForMapToUpperCaseList(String sql, Class<T> elementType) throws Exception {
        List<T> arrayList = new ArrayList<>();
        List<Map<String, Object>> maps = super.queryForList(sql);
        for (Map<String, Object> m : maps) {
            T t = MapToObj.mapToObjUpperCase(m, elementType);
            arrayList.add(t);
        }
        return arrayList;
    }


    public <T> List<T> queryForMapToList(String sql, Object[] objects, int[] types, Class<T> elementType) throws Exception {
        List<T> arrayList = new ArrayList<>();
        List<Map<String, Object>> maps = super.queryForList(sql,objects, types);
        for (Map<String, Object> m : maps) {
            T t = MapToObj.mapToObj(m, elementType);
            arrayList.add(t);
        }
        return arrayList;
    }
}
